import * as Cesium from "cesium";
import { paths } from "../libs/data";
let datas = [
  // [117.4603186710001, 31.14388249900003, 11.147400000001653],
  // [117.4594623780001, 31.143739847000063, 11.10839999999346],
  // [117.4585990680001, 31.143571198000075, 10.89079999999376],
  // [117.45789337300005, 31.143422075000046, 11.12170000000333],
  // [117.4571119630001, 31.143350937000037, 11.545700000002398],
  // [117.45620292500007, 31.14332503000028, 11.529899999994086],
  // [117.45545284400009, 31.143363754000063, 10.03810000005215],
  // [117.45473256600008, 31.143448056000068, 10.8638000000645],
  // [117.45399052200003, 31.143623321000064, 11.34560000000559],
  // [117.45347615200001, 31.14381135600007, 11.68730000005052],
  // [117.45292459000007, 31.144031608000034, 12.10610000004517],
  // [117.45192097000006, 31.14426226000064, 12.84239999994086],
  // [117.45065835500009, 31.144954275000032, 12.71229999999232],
  // [117.44980033200011, 31.145266268000057, 12.50489999999907],
  // [117.44943370300007, 31.145413392000023, 12.73159999999162],
  // [117.44920128900003, 31.145382554000037, 12.96769999993783],
  // [117.44897692800009, 31.144980649000047, 14.90959999999045],
  // [117.44872415000009, 31.14449598400006, 14.5589999999383],
  // [117.44851592000009, 31.144125416000065, 14.41099999992782],
  // [117.44848024700002, 31.14392828000007, 14.4758000000163],
  // [117.44948683700011, 31.14350793500006, 14.50740000002235],
  // [117.45089297600009, 31.142959855000072, 14.29039999998044],
  // [117.45149371900004, 31.142693826000027, 14.12709999997881],
  // [117.45166848000008, 31.142571364000048, 15.5261000000277],
  // [117.4516358520001, 31.142433625000024, 14.034100000044],
  // [117.45082070700005, 31.140899211000033, 13.28909999994505],
];
datas = paths;
export const init = () => {
  const viewer = new Cesium.Viewer("cesiumContainer", {
    animation: false,
    fullscreenButton: false,
    vrButton: false,
    geocoder: false,
    homeButton: false,
    sceneModePicker: false,
    timeline: false,
    shouldAnimate: true,
    // terrain: Cesium.Terrain.fromWorldTerrain()
    terrain: new Cesium.Terrain(Cesium.CesiumTerrainProvider.fromUrl("model/yunnan"))
    // terrain: new Cesium.Terrain(Cesium.CesiumTerrainProvider.fromUrl("http://106.52.5.98:3000/geoserver/ne/wms"))
  });

  // 配置天地图的瓦片服务
  const vectorMapProvider = new Cesium.WebMapTileServiceImageryProvider({
    url: `http://t0.tianditu.gov.cn/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=444343654c884379c0d692682e2b6f67`,
    layer: "tdtVecBasicLayer",
    style: "default",
    format: "image/jpeg",
    tileMatrixSetID: "GoogleMapsCompatible",
    // show: false
  });
  // const tdtCiaProvider = new Cesium.WebMapTileServiceImageryProvider({
  //   url: ``,
  //   layer: "tdtVecBasicLayer",
  //   style: "default",
  //   format: "image/jpeg",
  //   tileMatrixSetID: "GoogleMapsCompatible",
  //   // show: false
  // });
const tdtCiaProvider = new Cesium.WebMapTileServiceImageryProvider({
    url: `http://t{s}.tianditu.gov.cn/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=444343654c884379c0d692682e2b6f67`,
    subdomains: ['0', '1', '2', '3'],
    layer: 'tdtCiaLayer',
    style: 'default',
    tileMatrixSetID: 'w',
    maximumLevel: 18
});
viewer.imageryLayers.addImageryProvider(vectorMapProvider);
viewer.imageryLayers.addImageryProvider(tdtCiaProvider);

// viewer.shadows = true;
viewer.scene.globe.enableLighting = true;
 
// viewer.imageryLayers.add(tdtLayer);

  // let imagel = new Cesium.ImageryLayer(
  //   new Cesium.UrlTemplateImageryProvider({
  //     // url: "http://webst01.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}",
  //     url: "http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=444343654c884379c0d692682e2b6f67",
  //     minimumLevel: 0,
  //     maximumLevel: 18
  //   })
  // );
  // viewer.imageryLayers.add(imagel);
  // 添加到 viewer 中
  // viewer.imageryLayers.addImageryProvider(vectorMapProvider);
  // viewer.imageryLayers.addImageryProvider(tdtCiaProvider);
  viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(102.72138431570705, 24.855518676645523, 6000),

    model: {
      uri: "model/glb/Cesium_Air.glb",
      minimumPixelSize: 100,
      show: true
    },
    orientation: Cesium.Transforms.headingPitchRollQuaternion(Cesium.Cartesian3.fromDegrees(102.77138431570705, 24.965518676645523, 6000), new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(30), Cesium.Math.toRadians(15), 0))
    // orientation: Cesium.Transforms.headingPitchRollQuaternion(Cesium.Cartesian3.fromDegrees(116.391, 39.907, 1000), new Cesium.HeadingPitchRoll(0, Cesium.Math.toRadians(45), 0))
  });

  const dataSource = new Cesium.CustomDataSource("entetiesdata");
  viewer.dataSources.add(dataSource);
  dataSource.entities.add({
    name: "line",
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(datas.flat()),
      width: 10,
      material: Cesium.Color.RED,
      clampToGround: true
    }
  });
  const step = 100;
  const property = new Cesium.SampledPositionProperty();
  const startTime = new Date().getTime();
  const lujudata = datas.map((pos, i) => {
    let time = new Date(startTime + i * step);
    // console.log(viewer.scene.globe.ellipsoid.cartesianToCartographic(Cesium.Cartesian3.fromDegrees(pos[0], pos[1])));
    // // let height = viewer.scene.globe.ellipsoid.cartesianToCartographic(Cesium.Cartesian3.fromDegrees(pos[0], pos[1])).height || 0;
    let position = Cesium.Cartesian3.fromDegrees(pos[0], pos[1]);
    property.addSample(Cesium.JulianDate.fromDate(time), position);
    return pos;
  });
  let stopTime = startTime + (lujudata.length - 1) * step;
  property.setInterpolationOptions({
    interpolationDegree: 0.001,
    interpolationAlgorithm: Cesium.LinearApproximation
  });
  let entitiedd = viewer.entities.add({
    availability: new Cesium.TimeIntervalCollection([
      new Cesium.TimeInterval({
        start: Cesium.JulianDate.fromDate(new Date(startTime)),
        stop: Cesium.JulianDate.fromDate(new Date(stopTime))
      })
    ]),

    heightReference: Cesium.HeightReference.CLAMP_TO_TERRAIN,
    position: property,
    path: {
      leadTime: 0,
      resolution: 1,
      width: 10,
      clampToGround: true,

      material: new Cesium.PolylineGlowMaterialProperty({
        color: Cesium.Color.WHITE
      })
    },
    // polyline: {
    //   positions: property,
    //   width: 5,
    //   material: new Cesium.PolylineGlowMaterialProperty({
    //     color: Cesium.Color.WHITE,
    //   }),
    //   clampToGround: true,
    //   heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
    // },
    label: {
      text: "",
      fillColor: Cesium.Color.RED,
      pixelOffset: new Cesium.Cartesian2(0, -60)
      // orientation: new Cesium.VelocityOrientationProperty(property)
    },
    model: {
      uri: "model/glb/su7.glb",
      minimumPixelSize: 100,
      maximumPixelSize: 100,

      silhouetteColor: Cesium.Color.GREEN,
      silhouetteSize: 1,
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
    },
    orientation: new Cesium.VelocityOrientationProperty(property)
    // scale: 1.5,
    // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  });
  console.log(startTime, stopTime);
  viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date(startTime));
  viewer.clock.stopTime = Cesium.JulianDate.fromDate(new Date(stopTime));
  viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
  viewer.clock.shouldAnimate = true;
  // viewer.clock.onTick.addEventListener(function (tick) {
  //   let s = entitiedd.position.getValue(tick.currentTime);
  //   console.log(s);
  //   let graphic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(s);
  //   graphic.longitude = Cesium.Math.toDegrees(graphic.longitude);
  //   graphic.latitude = Cesium.Math.toDegrees(graphic.latitude);
  //   console.log(graphic);
  //   entitiedd.label.text = `${Number(graphic.longitude).toFixed(4)},${Number(graphic.latitude).toFixed(4)}`;
  // });
  viewer.zoomTo(dataSource);

  // const viewer = new Cesium.Viewer("cesiumContainer", {
  //   clampToGround: true,
  //   terrain: Cesium.Terrain.fromWorldTerrain(),
  // });
  // // const provider = new Cesium.WebMapServiceImageryProvider({
  // //   url: "/geoserver/gwc/service/wms",
  // //   parameters: {
  // //     transparent: true,
  // //     format: "image/png",
  // //   },
  // // });
};
